//
//  GamePad.hpp
//  Snake
//
//  Created by CmST0us on 16/11/25.
//  Copyright © 2016年 SpringEngine. All rights reserved.
//

#ifndef GamePad_hpp
#define GamePad_hpp

#include <stdio.h>
#include "Snake.hpp"
#include <event2/event.h>
#include "Food.hpp"
#include <semaphore.h>

typedef event_base* EventBaseRef;
typedef event*      EventRef;
typedef evutil_socket_t EventFd;
class GamePad{
public:
    static const char keyUP = 'w';
    static const char keyDown = 's';
    static const char keyLeft = 'a';
    static const char keyRight = 'd';
    static const char keySpeedUp = 'p';
    static const char keySpeedDown = 'l';
private:
    GamePad();
public:
    static GamePad* shareInstance();

    Snake _snake;
    FoodRef _food;
    EventBaseRef _eventBase;
    
private:
//    sem_t* _sem;
//    int postTimes;
    Frame _frame;
    int _speedValue;
public:
    void run();
    void listenKeyInput();
    static void gamePadWillUpdateView(EventFd fd, short event, void* ctx);
    
private:
    void reDrawPlayGround();
    Point generateFoodPoint();
    void endGame();
};

typedef GamePad* GamePadRef;
#endif /* GamePad_hpp */
